<?php

class Manager extends CI_Controller {

	public function __construct() {
		parent::__construct ();
		$this->types = array('none', 'ipad', 'ipad', 'iphone', 'iphone', '91', '91');
		$this->prefix = array('none', 'cover_gril', 'pk', 'cover_gril', 'pk', 'cover_gril', 'pk');
		$this->dbs = array('', 'ipad_cover_gril', 'ipad_pk', 'iphone_cover_gril', 'iphone_pk', '91_cover_gril', '91_pk');

		$this->msg = '';
	}

	private function update_period( $type ) {
		$db = $this->load->database ( $this->dbs[$type], true );
		
		$period = $_GET['target'];
		$property = $_GET['property'];
		$new_vaule = $_GET['new_value'];
		$db->query("UPDATE ".$this->prefix[$type]."_competition SET $property = '$new_vaule' WHERE period = $period");
		
		if ($db->affected_rows() <= 0) {
			$this->msg = '操作失败';
		}
		//$this->load->helper('url');
		//redirect(get_config('base_url')."type=$type&protocal=1001", 'location', 302);
		$this->show_period( $type );
	}
	private function delete_period( $type ) {
		$db = $this->load->database ( $this->dbs[$type], true );
		
		$period = $_GET['target'];

		log_message('debug', "delete_period $period " );
		$db->query("DELETE FROM ".$this->prefix[$type]."_competition WHERE period = $period");
		
		if ($db->affected_rows() <= 0) {
			$this->msg = '操作失败';
		}
		$this->show_global($type);
	}
	private function add_period( $type ) {
		$db = $this->load->database ( $this->dbs[$type], true );
		
		$period = $_POST['period'];
		$start_time = $_POST['start_time'];
		$end_time = $_POST['end_time'];
		
		log_message('debug', "add_period $period $start_time $end_time" );
		$db->query("INSERT INTO ".$this->prefix[$type]."_competition(period, start_time, end_time) VALUES( $period, '$start_time', '$end_time' )");
		
		if ($db->affected_rows() <= 0) {
			$this->msg = '操作失败';
		}
		$this->show_global($type);
	}
	
	private function cover_gril_vote( $type ) {
		$db = $this->load->database ( $this->dbs[$type], true );
		
		$userid = $_GET['target'];
		$number = $_GET['number'];
		
		log_message('debug', "cover_gril_vote $userid $number" );
		$db->query("call proc_vote_n($userid, $number)");
		
		//$this->load->helper('url');
		//redirect(get_config('base_url')."type=$type&protocal=1001", 'location', 302);
		$this->show_current_period($type);
	}
	private function cover_gril_shield( $type ) {
		$db = $this->load->database ( $this->dbs[$type], true );
		
		$userid = $_GET['target'];
		$shield = $_GET['shield'];
		
		log_message('debug', "cover_gril_shield $userid $shield" );
		$db->query("UPDATE cover_gril SET shield = $shield WHERE userid = $userid ");
		
		if ($db->affected_rows() <= 0) {
			$this->msg = '操作失败';
		}
//		$this->load->helper('url');
//		redirect(get_config('base_url')."type=$type&protocal=1001", 'localtion', 302);
		$this->show_current_period($type);
		
	}
	
	
	
	public function show_global( $type ) {

		$data['type'] = $type;
		$data['title'] = $this->types[$type].' '.$this->prefix[$type].' 全局信息';
		$data['msg'] = $this->msg;
		
		$db = $this->load->database ( $this->dbs[$type], true );
		$query = $db->query("SELECT period, start_time, end_time, number FROM ".$this->prefix[$type].'_competition WHERE state >= 1 ORDER BY period ASC');
		$data['period_list'] = $query->result();
		
		$this->load->view('index', $data);
	}

	public function show_period( $type ) {

		$data['type'] = $type;
		$data['title'] = $this->types[$type].' '.$this->prefix[$type].' 期数时间';
		$data['msg'] = $this->msg;
		
		$db = $this->load->database ( $this->dbs[$type], true );
		$query = $db->query("SELECT period, start_time, end_time FROM ".$this->prefix[$type].'_competition WHERE state <= 1 ORDER BY period ASC');
		$data['period_list'] = $query->result();

		$this->load->view('period', $data);
	}

	public function show_current_period( $type ) {

		$data['type'] = $type;
		$data['title'] = $this->types[$type].' '.$this->prefix[$type].' 本期比赛';
		$data['msg'] = $this->msg;
		
		if ($type == 1 || $type == 3 || $type == 5) {

			$db = $this->load->database ( $this->dbs[$type], true );
			$sql = "SELECT period, user.userid, user.username, number_vote, ranking, latest_time_vote, create_time, shield FROM ".$this->prefix[$type]." INNER JOIN user ON ".$this->prefix[$type].".userid = user.userid ";
			if (isset($_POST['username'])) {
				$username = $_POST['username'];
				$sql .= " WHERE username LIKE '%" . $db->escape_like_str ( $username ) . "%' ";
			}
			if (isset($_GET['order'])) {
				$order = $_GET['order'];
				$direct = $_GET['direct'];
				$sql .= " ORDER BY $order $direct ";
			}
			$query = $db->query( $sql );
			$data['cover_gril_list'] = $query->result();
			
			$this->load->view('cover_gril/current_period.php', $data);
		}
		else {
			
		}
	}
	public function index()
	{
		$type = $_GET['type'];
		$protocal = $_GET['protocal'];


		
		log_message('debug', $protocal);

		switch ($protocal) {
			case 1001 :
				$this->show_global($type);
				break;
			case 1002 :
				break;
			case 1004 :
				$this->show_period($type);
				break;
			case 1005 :
				$this->show_current_period($type);
				break;



			case 1109 :
				$this->update_period($type);
				break;
			case 1110 :
				$this->delete_period($type);
				break;
			case 1103 :
				$this->add_period($type);
				break;
				
			case 1201 :
				$this->cover_gril_vote( $type );
				break;
			case 1202 :
				$this->cover_gril_shield( $type );
				break;
				
		}
	}
}
?>